/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.LoanBillBean;
import com.minierp.dao.LoanBillDao;
import com.minierp.model.LoanBill;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class LoanBillValidator implements Validator {
	private LoanBillDao loanBillDao;

	public boolean supports(Class clazz) {
		return LoanBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		LoanBillBean bean = (LoanBillBean) obj;
		LoanBill form = bean.getLoanBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("loanBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getOldId() != 0L || bean.getCodeRule()
						.getGenerateRule().equals("MANUAL"))) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				LoanBill nameStr = this.loanBillDao.get(form.getBillCode());
				if (nameStr != null && nameStr.getId() != form.getId()) {
					errors.rejectValue("loanBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("loanBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				form.setLoanDate(sf.parse(bean.getLoanDateStr()));
			} catch (Exception arg10) {
				hasError = true;
				errors.rejectValue("loanDateStr", "",
						"『日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		String nameStr1 = "";
		String rpName = "";
		if (form.getBillType().equals("IN")) {
			nameStr1 = "收款";
			rpName = "付款单位";
		} else {
			nameStr1 = "付款";
			rpName = "收款单位";
		}

		if (!hasError) {
			if (form.getDepartmentId() == 0L) {
				errors.rejectValue("loanBill.departmentId", "", "请选择『部门』");
				hasError = true;
			} else if (form.getBankAccountId() == 0L) {
				errors.rejectValue("loanBill.bankAccountId", "", "请选择『"
						+ nameStr1 + "账户』");
				hasError = true;
			} else if (form.getManagerId() == 0L) {
				errors.rejectValue("loanBill.managerId", "", "请选择『经办人员』");
				hasError = true;
			} else {
				try {
					form.setCost(new BigDecimal(bean.getCostStr()));
				} catch (Exception arg9) {
					hasError = true;
					errors.rejectValue("costStr", "", "『" + nameStr1
							+ "金额』只能输入数字");
				}
			}
		}

		if (!hasError
				&& (form.getRpName() == null || form.getRpName().trim()
						.equals(""))) {
			hasError = true;
			errors.rejectValue("loanBill.rpName", "", "请输入『" + rpName + "』");
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 2000) {
			hasError = true;
			errors.rejectValue("loanBill.mark", "", "『摘要』不能超过2000个字符");
		}

	}

	public LoanBillDao getLoanBillDao() {
		return this.loanBillDao;
	}

	public void setLoanBillDao(LoanBillDao loanBillDao) {
		this.loanBillDao = loanBillDao;
	}
}